package com.juzipi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.juzipi.common.model.PageBody;
import com.juzipi.common.tools.PageResult;
import com.juzipi.common.utils.StringUtils;
import com.juzipi.inter.bo.UserBo;
import com.juzipi.inter.mapper.UserMapper;
import com.juzipi.inter.pojo.User;
import com.juzipi.inter.vo.UserRoleVo;
import com.juzipi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;


@Service
@Primary
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {


    @Autowired
    private UserMapper userMapper;




    /**
     * 查询用户返回分页结果集
     * @param pageBody
     * @return
     */
    @Override
    public PageResult queryAllUserAndRole(PageBody pageBody) {
        Page<UserRoleVo> page = userMapper.queryUserAndRoleByPage(new Page<User>(pageBody.getPageNum(), pageBody.getPageSize()));
        return PageResult.pageResult(page.getTotal(),page.getPages(),page.getRecords());
    }


    /**
     * 修改用户，部分数据修改时需要检查是否已存在
     * @param user
     * @return
     */
    @Transactional
    @Override
    public Object modifyUser(User user) {
        if (!checkEmailUnique(user.getEmail())) {
            return user.getEmail();
        }
        if (!checkPhoneNumberUnique(user.getPhoneNumber())){
            return user.getPhoneNumber();
        }
        return userMapper.updateById(user);
    }


    /**
     * 根据用户名模糊查询返回分页排序结果集
     * @param pageBody
     * @return
     */
    @Override
    public PageResult queryAllUserAndRoleLike(PageBody pageBody) {
        Page<UserRoleVo> page = userMapper.queryUserAndRoleByLike(
                new Page<UserRoleVo>(pageBody.getPageNum(), pageBody.getPageSize()), pageBody.getKey(), pageBody.getOrderBy());
        System.out.println("-------------------------------------");
        System.out.println(page.getTotal());
        System.out.println(page.getOrders());
        System.out.println(page.getRecords());
        System.out.println("---------------------");
        return PageResult.pageResult(page.getTotal(),page.getPages(),page.getRecords());
    }


    /**
     * 根据角色查询其所有用户的分页结果集
     * @param pageBody
     * @return
     */
    @Override
    public PageResult queryUserBySort(PageBody pageBody) {
        Page<UserBo> page = userMapper.queryUserBySort(new Page<>(pageBody.getPageNum(), pageBody.getPageSize()), pageBody.getKey());
        return PageResult.pageResult(page.getTotal(),page.getPages(),page.getRecords());
    }


    /**
     * 根据id批量删除用户
     * @param userIds
     * @return
     */
    @Override
    public Integer removeUserByIds(List<Long> userIds) {
        return userMapper.deleteBatchIds(userIds);
    }


    /**
     * 根据id查询用户
     * @param userId
     * @return
     */
    @Override
    public UserBo queryUserById(Long userId) {
        return userMapper.queryUserById(userId);
    }



    /**
     * 判断用户名是否存在
     * @param username
     * @return
     */
    @Override
    public Boolean checkUsernameIsExist(String username) {
        //根据用户名查数据库得到用户,判断得到的用户(为空就是没有直接返回唯一)和传来的用户id是否相同，相同就表示是同一个用户的，返回唯一，不同就是其他人的，返回不唯一
        User userInfo = userMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getUsername, username));
        return StringUtils.isNull(userInfo);
    }


    /**
     * 验证邮箱是否唯一
     * @param email
     * @return
     */
    @Override
    public Boolean checkEmailUnique(String email) {
        User userInfo = userMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getEmail, email));
        return StringUtils.isNull(userInfo);
    }


    /**
     * 验证手机号是否唯一
     * @param phoneNumber
     * @return
     */
    @Override
    public Boolean checkPhoneNumberUnique(String phoneNumber) {
        User userInfo = userMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getPhoneNumber, phoneNumber));
        return !StringUtils.isNotNull(userInfo) || Objects.equals(userInfo.getPhoneNumber(), phoneNumber);
    }


}
